package com.pipayshop.utils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
//对象字段非空负数判断
public class ObjectValidator {

    public static void validate(Object obj) throws IllegalArgumentException, IllegalAccessException {
        if (obj == null) {
            throw new IllegalArgumentException("Object cannot be null.");
        }

        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true); // Allow access to private fields
            Object value = field.get(obj); // Get field value

            if (value == null) {
                // Ensure non-null for specific types
                if (field.getType() == String.class || Number.class.isAssignableFrom(field.getType())) {
                    throw new IllegalArgumentException("Field " + field.getName() + " cannot be null.");
                }
                continue; // Skip other types
            }

            if (value instanceof String) {
                String strValue = (String) value;
                if (strValue.isEmpty()) {
                    throw new IllegalArgumentException("Field " + field.getName() + " cannot be empty.");
                }
            } else if (value instanceof Number) {
                if (value instanceof Integer && (Integer) value <= 0) {
                    throw new IllegalArgumentException("Field " + field.getName() + " cannot be less than or equal to 0.");
                } else if (value instanceof Long && (Long) value <= 0) {
                    throw new IllegalArgumentException("Field " + field.getName() + " cannot be less than or equal to 0.");
                } else if (value instanceof BigDecimal && ((BigDecimal) value).compareTo(BigDecimal.ZERO) <= 0) {
                    throw new IllegalArgumentException("Field " + field.getName() + " cannot be less than or equal to 0.");
                }
            }
        }


    }
    public static void main(String[] args) {
        MyClass validObj = new MyClass("John", 25, BigDecimal.valueOf(100.5), 10L);
        MyClass invalidObj1 = new MyClass(null, 25, BigDecimal.valueOf(100.5), 10L); // name is null
        MyClass invalidObj2 = new MyClass("John", null, BigDecimal.valueOf(100.5), 10L); // age is null
        MyClass invalidObj3 = new MyClass("John", 25, null, 10L); // balance is null
        MyClass invalidObj4 = new MyClass("John", 25, BigDecimal.valueOf(-10.5), 10L); // balance is negative
        try {
            ObjectValidator.validate(validObj);
            System.out.println("Valid object passed validation.");
        } catch (IllegalArgumentException | IllegalAccessException e) {
            System.err.println("Validation failed: " + e.getMessage());
        }

        try {
            ObjectValidator.validate(invalidObj1);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            System.err.println("Validation failed: " + e.getMessage());
        }

        try {
            ObjectValidator.validate(invalidObj2);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            System.err.println("Validation failed: " + e.getMessage());
        }

        try {
            ObjectValidator.validate(invalidObj3);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            System.err.println("Validation failed: " + e.getMessage());
        }

        try {
            ObjectValidator.validate(invalidObj4);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            System.err.println("Validation failed: " + e.getMessage());
        }
    }
}
class MyClass {
    private String name;
    private Integer age;
    private BigDecimal balance;
    private Long id;

    public MyClass(String name, Integer age, BigDecimal balance, Long id) {
        this.name = name;
        this.age = age;
        this.balance = balance;
        this.id = id;
    }
}

